<?php

namespace Biz\service\GoodsTestActivity;

use Biz\model\GoodsTestActivity\FineGoodsJoinModel;
use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\service\brand\BrandActivity;
use Doraemon\cache\Queue;
use Doraemon\model\Sysmsg;
use Doraemon\service\User;
use Doraemon\tools\LinkFormat;
use Doraemon\tools\Log;
use Exception;

class PushNoticeService
{
    /**
     * @param $courierId
     * @param $trackingNumber
     * @param $id
     * @param $prizeName
     * @param $courierName
     * @param int $uid
     * @return bool
     */
    public static function afterSendGoods(
        $courierId,
        $trackingNumber,
        $id,
        $prizeName,
        $courierName,
        int $uid = 0
    ): bool
    {
        try {
            FineGoodsJoinModel::update([
                'courier_id' => $courierId,
                'tracking_number' => $trackingNumber,
                'logistics_status' => FineGoodsJoinModel::LOGISTICS_STATUS_SHIPPED,
            ], ['id' => $id]);

            //获取活动id
            $activityId = FineGoodsJoinModel::get('fine_goods_id', ['id' => $id]);
            $activityInfo = FineGoodsModel::first(['id' => $activityId],['type']);
            $activityTypeName = FineGoodsModel::TYPE_MAP[$activityInfo['type']];
            //发送通知
            $detail = "{$activityTypeName}产品「" . $prizeName . "」已寄出，物流公司：" . $courierName . ",物流单号：" . $trackingNumber . "，请注意查收。";
            self::pushSysMsg($uid,
                $activityTypeName.'产品已寄出',
                $detail,
                User::HAOHAOZHUFULIGUAN_UID,
                Sysmsg::TYPE_NOTICE,
                $activityId
            );

            return true;
        } catch (Exception $e) {
            Log::error('创建物流单号异常', $e->getMessage());

            return false;
        }
    }

    /**
     * @param $uid
     * @param $title
     * @param $detail
     * @param $fromUid
     * @param $type
     * @param int $activityId
     */
    public static function pushSysMsg($uid, $title, $detail, $fromUid, $type, int $activityId = 0)
    {
        if ($activityId) {
            $link = BrandActivity::getFindGoodsTestLink($activityId);
            $link = LinkFormat::encryptionLink($link);
        } else {
            $link = '';
        }
        if ($uid > 0) {
            $data = [
                'msgCode' => 0,
                'to_uid' => $uid,
                'msgParams' => [
                    'from_uid' => $fromUid,
                    'title' => $title,
                    'detail' => $detail,
                    'link' => $link,
                    'type' => $type,
                ],
            ];
            Log::info('service/GoodsTestActivity/PushNoticeService.php-pushSysMsg',$data);
            Queue::insertQueue('queue/Sysmsg/Send', $data,
                Queue::QUEUE_DORAEMON);
        }
    }


    /**
     * @param $uid
     * @param $fineGoodsId
     */
    public static function pushImMsg($uid,$fineGoodsId)
    {
        //参考文档 https://confluence.haohaozhu.me/pages/viewpage.action?pageId=96469088
        try {
            if ($uid > 0) {
                $nickName = User::getUser($uid)['nick'];
                $activityInfo = FineGoodsModel::first(['id' => $fineGoodsId],['type']);
                $activityTypeName = FineGoodsModel::TYPE_MAP[$activityInfo['type']];
                if ($fineGoodsId == 35) { //0903需求 对此活动特殊处理 内容中的7天改为15天
                    $date = '15';
                }else{
                    $date = '7';
                }
                $str = 'hi，' . $nickName . "，请于收到{$activityTypeName}产品后".$date."日内进行使用评测，并产出图文内容1篇。

{$activityTypeName}内容要求：
1. 原创、真实的使用感受
2. 文字描述不少于300字，图片不少于9张，关联品牌标签及商品链接，特别注意逾期未发布好好住将有权回收{$activityTypeName}产品，并由{$activityTypeName}用户承担相应产品折旧费和物流费。内容逾期未发布将取消后续好好测资格及在好好住平台其他活动的参与权。";
                $msgInfo = [
                    'send_user' => User::HAOHAOZHUFULIGUAN_UID,
                    'con_type' => 0,
                    // 0 文字 1 图片 2卡片
                    'content' => json_encode([
                        'con' => $str,
                    ]),
                ];
                Log::info('service/GoodsTestActivity/PushNoticeService.php-pushImMsg',$msgInfo);
                Queue::insertQueue('queue/im/send', [
                    'to_uids' => [$uid],
                    'msgInfo' => $msgInfo,
                ], Queue::QUEUE_DORAEMON);
            }
        } catch (Exception $exception) {
            Log::error('发送私信错误(PushNoticeService-pushPrivateLetters) ', $exception->getMessage());

        }

    }
}